package com.vueapi.controller.admin;

import com.vueapi.annotation.OperationLog;
import com.vueapi.domain.HjUser;
import com.vueapi.domain.dto.PasswordDTO;
import com.vueapi.domain.vo.UserInfoVo;
import com.vueapi.service.HjUserService;
import com.vueapi.utils.ResultUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @Classname AdminUserController
 * @Description TODO
 * @Created by 赵宏建
 * @Date 2022/9/21 9:38
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/admin/user")
public class AdminUserController {

    @Resource
    private HjUserService userService;

    @GetMapping("list")
    public ResultUtils list() {
        return ResultUtils.success(userService.list());
    }

    @OperationLog(name = "修改用户信息")
    @PutMapping("edit")
    public ResultUtils edit(@RequestBody HjUser user){
        userService.updateById(user);
        return ResultUtils.success();
    }

    @OperationLog(name = "修改用户密码")
    @PutMapping("editPassword")
    public ResultUtils editPassword(@RequestBody PasswordDTO passwordDTO) {
        return userService.editPassword(passwordDTO);
    }

    @GetMapping("getUserInfoByToken")
    public ResultUtils getUserInfoByToken(HttpServletRequest request){
        UserInfoVo user = userService.getUserInfoByToken(request.getHeader("Authorization"));
        if(user == null){
            return ResultUtils.fail("当前登录用户已过期，请重新登录！");
        }
        return ResultUtils.success(user);
    }

}
